home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / langs / iconv8_l.arc / PROGS.ARC / empg.icn < prev    next >
Encoding:
Text File  |  1990-03-08  |  5.8 KB  |  154 lines

  1. ############################################################################
  2. #
  3. #    Name:    empg.icn
  4. #
  5. #    Title:    Expression Measurement Program Generator
  6. #
  7. #    Author:    Ralph E. Griswold
  8. #
  9. #    Date:    March 8, 1990
  10. #
  11. ############################################################################
  12. #
  13. #     This program reads Icon expressions, one per line, and writes out
  14. #  and Icon program, which when run, times the expressions and reports
  15. #  average evaluation time and storage allocation.
  16. #
  17. #     Lines beginning with a # are treated as comments and written to the
  18. #  output program so as to be written as comments when the output program is
  19. #  run.
  20. #
  21. #     Lines beginning with a : are passed to the output program to be
  22. #  evaluated, but not timed.
  23. #
  24. #     Lines beginning with a $ are included at the end of the output
  25. #  program as declarations.
  26. #
  27. #     All other lines are timed in loops.
  28. #
  29. #     An example of input is:
  30. #
  31. #    :T := table(0)
  32. #    $record complex(r,i)
  33. #    T[1]
  34. #    complex(0.0,0.0)
  35. #
  36. #     The resulting output program evaluates the expressions on the last two
  37. #  lines and reports their average time and storage allocation.
  38. #
  39. #     Loop overhead for timing is computed first. The default number of
  40. #  iterations s 10000. A different number can be given on the command line
  41. #  when empg is executed, as in
  42. #
  43. #    iconx empg 1000 <test.exp >test.icn
  44. #
  45. #  which takes expressions from test.exp, computes loop overhead using 1000
  46. #  iterations, and writes the measurement program to test.icn.
  47. #
  48. #     The default number of iterations for timing expressions is 1000. A
  49. #  different number can be given on the command line when the measurement
  50. #  program is run, as in
  51. #
  52. #    icont test
  53. #    iconx test 5000
  54. #
  55. #  which times the expressions in test.icn using 5000 iterations.
  56. #
  57. #     If a garbage collection occurs during timing, the average time is
  58. #  likely to be significantly distorted and average allocation cannot be
  59. #  computed.  In this case, the number of garbage collections is reported
  60. #  instead.  To avoid misleading results as a consequence, measurement
  61. #  programs should be run with Icon's region sizes set to as large values
  62. #  as possible. To avoid residual effects of one timed expression on
  63. #  another, expressions that allocate significant amounts of storage
  64. #  should be measured in separate programs.
  65. #
  66. #     The number of iterations used to compute loop overhead im empg
  67. #  and the number of iterations used to time expressions in measurement
  68. #  programs should be chosen so that the effects of low clock resolution
  69. #  are minimized.  In particular, systems with very fast CPUs but
  70. #  low clock resolution (like 386 and 486 processors running under
  71. #  MS-DOS) need large values.
  72. #
  73. ############################################################################
  74. #
  75. #  Links: numbers (in measurement programs, not in empg.icn)
  76. #
  77. ############################################################################
  78.  
  79. procedure main(argl)
  80.    local i, decls, line, input
  81.    i := integer(argl[1]) | 10000
  82.    decls := []                # list for declarations
  83.    write("link numbers")
  84.    write("global _Count, _Coll, _Store, _Overhead, _Names")
  85.    write("procedure main(argl)")
  86.    write("   _Iter := argl[1] | 1000")
  87.    write("   _Names := [\"static\",\"string\",\"block \"]")
  88.    write("   write(\"iterations: \",_Iter)")
  89.    write("   write(\"&version: \",&version)")
  90.    write("   write(\"&host: \",&host)")
  91.    write("   write(\"&dateline: \",&dateline)")
  92.    write("   write(\"region sizes: \")")
  93.    write("   _I := 1")
  94.    write("   every _S := ®ions do {")
  95.    write("      write(\"   \",_Names[_I],\"   \",_S)")
  96.    write("      _I +:= 1")
  97.    write("      }")
  98.    write("   _Count := ",i)
  99.    write("   _Itime := &time")
  100.    write("   every 1 to _Count do { &null }")
  101.    write("   _Overhead := real(&time - _Itime) / _Count")
  102.    write("   _Itime := &time")
  103.    write("   every 1 to _Count do { &null & &null }")
  104.    write("   _Overhead := real(&time - _Itime) / _Count - _Overhead")
  105.    write("   _Count := _Iter")
  106.    while line := read(input) do 
  107.       case line[1] of {
  108.          ":": {            # evaluate but do not time
  109.             write("   ",line[2:0])
  110.             write("   write(",image(line[2:0]),")")
  111.             }
  112.          "$": {            # line of declaration
  113.             put(decls,line[2:0])
  114.             write("   write(",image(line[2:0]),")")
  115.             }
  116.          "#":            # comment
  117.             write("   write(",image(line),")")
  118.          default: {        # time in a loop
  119.             write("   write(",image(line),")")
  120.             write("   _Prologue()")
  121.             write("   _Itime := &time")
  122.             write("   every 1 to _Count do {")
  123.             write("      &null & ", line)
  124.             write("      }")
  125.             write("   _Epilogue(&time - _Itime)")
  126.             }
  127.       }
  128.    write("end")
  129.    write("procedure _Prologue()")
  130.    write("   _Store := []")
  131.    write("   _Coll := []")
  132.    write("   collect()")
  133.    write("   every put(_Store,&storage)")
  134.    write("   every put(_Coll,&collections)")
  135.    write("end")
  136.    write("procedure _Epilogue(_Time)")
  137.    write("   every put(_Store,&storage)")
  138.    write("   every put(_Coll,&collections)")
  139.    write("   write(fix(real(_Time) / _Count - _Overhead,1,8),\" ms.\")")
  140.    write("   if _Coll[1] = _Coll[5] then {")
  141.    write("      write(\"average allocation:\",)")
  142.    write("         every _I := 1 to 3 do")
  143.    write("            write(\"   \",_Names[_I],fix(real(_Store[_I + 3] - _Store[_I]),_Count,12))")
  144.    write("      }")
  145.    write("   else {")
  146.    write("   write(\"garbage collections:\")")
  147.    write("   write(\"   total \",right(_Coll[5] - _Coll[1],4))")
  148.    write("   every _I := 6 to 8 do write(\"   \",_Names[_I - 5],right(_Coll[_I] - _Coll[_I - 4],4))")
  149.    write("      }")
  150.    write("   write()")
  151.    write("end")
  152.    every write(!decls)        # write out declarations
  153. end
  154.